<!DOCTYPE html>
<html lang=zh>
<head><meta name="generator" content="Hexo 3.9.0">
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="description" content="前言在做公司的运营管理后台页面创建工具的时候需要实现用户操作的撤销和还原，因为存储页面结构的数据量很多，不能都存储在内存中，所以考虑使用IndexedDB。开始的时候在npm里找IndexedDB有没有类似mongoose这样的简单易操作的库，结果翻了一圈愣是没有。好吧&amp;gt;_&amp;lt;，只能自己撸一个了（其实到了后来才理解为啥没有indexedDB的封装库，因为IndexedDB本来就是w3c创">
<meta name="keywords" content="工作">
<meta property="og:type" content="article">
<meta property="og:title" content="IndexedDB尝鲜">
<meta property="og:url" content="https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/index.html">
<meta property="og:site_name" content="月光倾城的代码小屋">
<meta property="og:description" content="前言在做公司的运营管理后台页面创建工具的时候需要实现用户操作的撤销和还原，因为存储页面结构的数据量很多，不能都存储在内存中，所以考虑使用IndexedDB。开始的时候在npm里找IndexedDB有没有类似mongoose这样的简单易操作的库，结果翻了一圈愣是没有。好吧&amp;gt;_&amp;lt;，只能自己撸一个了（其实到了后来才理解为啥没有indexedDB的封装库，因为IndexedDB本来就是w3c创">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://img.vim-cn.com/27/bc771fe03e705759c83ecabd4b34a74d0ce937.jpg">
<meta property="og:updated_time" content="2019-03-28T17:41:14.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="IndexedDB尝鲜">
<meta name="twitter:description" content="前言在做公司的运营管理后台页面创建工具的时候需要实现用户操作的撤销和还原，因为存储页面结构的数据量很多，不能都存储在内存中，所以考虑使用IndexedDB。开始的时候在npm里找IndexedDB有没有类似mongoose这样的简单易操作的库，结果翻了一圈愣是没有。好吧&amp;gt;_&amp;lt;，只能自己撸一个了（其实到了后来才理解为啥没有indexedDB的封装库，因为IndexedDB本来就是w3c创">
<meta name="twitter:image" content="https://img.vim-cn.com/27/bc771fe03e705759c83ecabd4b34a74d0ce937.jpg">
    
    
        
          
              <link rel="shortcut icon" href="/images/favicon.ico">
          
        
        
          
            <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
          
        
        
          
            <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
          
        
    
    <!-- title -->
    <title>IndexedDB尝鲜</title>
    <!-- styles -->
    <link rel="stylesheet" href="/css/style.css">
    <!-- persian styles -->
    
      <link rel="stylesheet" href="/css/rtl.css">
    
    <!-- rss -->
    
    
</head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" href="/2019/05/05/highcharts_record/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" href="/2019/03/21/react_drag_scrollbar/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">上一篇</span>
      <span id="i-next" class="info" style="display:none;">下一篇</span>
      <span id="i-top" class="info" style="display:none;">返回顶部</span>
      <span id="i-share" class="info" style="display:none;">分享文章</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/&text=IndexedDB尝鲜"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/&title=IndexedDB尝鲜"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/&is_video=false&description=IndexedDB尝鲜"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=IndexedDB尝鲜&body=Check out this article: https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/&title=IndexedDB尝鲜"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/&title=IndexedDB尝鲜"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/&title=IndexedDB尝鲜"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/&title=IndexedDB尝鲜"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/&name=IndexedDB尝鲜&description="><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#前言"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#IndexedDB的使用"><span class="toc-number">2.</span> <span class="toc-text">IndexedDB的使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#结束"><span class="toc-number">3.</span> <span class="toc-text">结束</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        IndexedDB尝鲜
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">月光倾城的代码小屋</span>
      </span>
      
    <div class="postdate">
        <time datetime="2019-03-28T09:41:14.000Z" itemprop="datePublished">2019-03-28</time>
    </div>


      

      
    <div class="article-tag">
        <i class="fas fa-tag"></i>
        <a class="tag-link" href="/tags/工作/">工作</a>
    </div>


    </div>
  </header>
  
<div class="article-gallery">
    
    <a class="gallery-item" href="https://img.vim-cn.com/27/bc771fe03e705759c83ecabd4b34a74d0ce937.jpg" rel="gallery_ck5exevqj0018ayoj78e6y7sy">
        <img src="https://img.vim-cn.com/27/bc771fe03e705759c83ecabd4b34a74d0ce937.jpg" itemprop="image" />
    </a>
    
</div>


  <div class="content" itemprop="articleBody">
    <h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>在做公司的运营管理后台页面创建工具的时候需要实现用户操作的撤销和还原，因为存储页面结构的数据量很多，不能都存储在内存中，所以考虑使用<code>IndexedDB</code>。<br>开始的时候在<code>npm</code>里找<code>IndexedDB</code>有没有类似<code>mongoose</code>这样的简单易操作的库，结果翻了一圈愣是没有。好吧&gt;_&lt;，只能自己撸一个了（其实到了后来才理解为啥没有<code>indexedDB</code>的封装库，因为<code>IndexedDB</code>本来就是<code>w3c</code>创建的用来记录客户端复杂数据的工具，w3c已经封装得很好了，所以就没有必要再封装了）。<br>在网上搜索<code>IndexedDB</code>最先找到的就是<code>MDN</code>的教程，奈何是纯英文的，没办法了只能靠自己的蹩脚英文去理解了，通过一段一段复制到google翻译，我终于大致学会了<code>IndexedDB</code>的使用，下面记录下自己的代码。</p>
<h3 id="IndexedDB的使用"><a href="#IndexedDB的使用" class="headerlink" title="IndexedDB的使用"></a>IndexedDB的使用</h3><p>因为工作很忙就不一一做解释了，大家看看注释理解下就行。<br>不过我想讲下自己的<code>IndexedDB</code>的数据库设计，因为需要记录用户的操作历史，所以我将每次用户改动之后的页面数据存在<code>data</code>里，然后使用当前编辑页面的唯一id–<code>page_id</code>来标识<code>data</code>，配合上<code>create_time</code>，意思就是页面id为<code>page_id</code>的页面中用户在<code>create_time</code>时刻的页面数据为<code>data</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * @Description: 存储用户改动历史的indexedDB操作类</span></span><br><span class="line"><span class="comment"> * @Author: lidikang</span></span><br><span class="line"><span class="comment"> * @LastEditors: lidikang</span></span><br><span class="line"><span class="comment"> * @Date: 2019-03-11 11:15:10</span></span><br><span class="line"><span class="comment"> * @LastEditTime: 2019-03-28 17:43:27</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">import</span> &#123; message &#125; <span class="keyword">from</span> <span class="string">'antd'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 兼容性处理</span></span><br><span class="line"><span class="keyword">const</span> thsIndexedDB =</span><br><span class="line">  <span class="built_in">window</span>.indexedDB || <span class="built_in">window</span>.mozIndexedDB || <span class="built_in">window</span>.webkitIndexedDB || <span class="built_in">window</span>.msIndexedDB;</span><br><span class="line"><span class="built_in">window</span>.IDBTransaction = <span class="built_in">window</span>.IDBTransaction ||</span><br><span class="line">  <span class="built_in">window</span>.webkitIDBTransaction ||</span><br><span class="line">  <span class="built_in">window</span>.msIDBTransaction || &#123; <span class="attr">READ_WRITE</span>: <span class="string">'readwrite'</span> &#125;; <span class="comment">// This line should only be needed if it is needed to support the object's constants for older browsers</span></span><br><span class="line"><span class="built_in">window</span>.IDBKeyRange = <span class="built_in">window</span>.IDBKeyRange || <span class="built_in">window</span>.webkitIDBKeyRange || <span class="built_in">window</span>.msIDBKeyRange;</span><br><span class="line"><span class="keyword">if</span> (!<span class="built_in">window</span>.indexedDB) &#123;</span><br><span class="line">  message.warning(<span class="string">'当前浏览器不支持IndexedDB， 请换成其他现代浏览器'</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="class"><span class="keyword">class</span> <span class="title">ThsIndexedDB</span> </span>&#123;</span><br><span class="line">  <span class="keyword">constructor</span>() &#123;</span><br><span class="line">    <span class="keyword">this</span>.db = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">this</span>.request = thsIndexedDB.open(<span class="string">'thsPageCreator'</span>, <span class="number">1</span>); <span class="comment">// 数据库名以及数据库版本，版本仅支持整数</span></span><br><span class="line">    <span class="keyword">this</span>.request.onerror = <span class="function"><span class="params">error</span> =&gt;</span> &#123;</span><br><span class="line">      message.error(<span class="string">'连接IndexedDB失败'</span>);</span><br><span class="line">      <span class="built_in">console</span>.warn(error);</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 连接成功，</span></span><br><span class="line"><span class="comment">     * 连接时异步的，所以请不要在new ThsIndexedDB()之后立马调用createData这些方法，因为此时的this.db还是null</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">this</span>.request.onsuccess = <span class="function"><span class="params">event</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="comment">// Do something with request.result!</span></span><br><span class="line">      <span class="keyword">this</span>.db = event.target.result;</span><br><span class="line">      <span class="keyword">this</span>.db.onerror = <span class="function"><span class="params">error</span> =&gt;</span> &#123;</span><br><span class="line">        message.error(<span class="string">'连接IndexedDB失败'</span>);</span><br><span class="line">        <span class="built_in">console</span>.warn(error);</span><br><span class="line">      &#125;;</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * onupgradeneeded更新数据库版本或者第一次创建数据库的时候被调用</span></span><br><span class="line"><span class="comment">     * 在这里创建和删除对象库，以及构建和删除索引</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">this</span>.request.onupgradeneeded = <span class="function"><span class="params">event</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">const</span> db = event.target.result;</span><br><span class="line">      <span class="comment">// 创建history表，并且使用自增键值，用来存储用户的操作记录</span></span><br><span class="line">      <span class="keyword">const</span> objectStore = db.createObjectStore(<span class="string">'history'</span>, &#123; <span class="attr">autoIncrement</span>: <span class="literal">true</span> &#125;);</span><br><span class="line"></span><br><span class="line">      objectStore.createIndex(<span class="string">'page_id'</span>, <span class="string">'page_id'</span>, &#123; <span class="attr">unique</span>: <span class="literal">false</span> &#125;); <span class="comment">// 标识文章id</span></span><br><span class="line">      objectStore.createIndex(<span class="string">'data'</span>, <span class="string">'data'</span>, &#123; <span class="attr">unique</span>: <span class="literal">false</span> &#125;); <span class="comment">// 标识design数据结构快照</span></span><br><span class="line">      objectStore.createIndex(<span class="string">'create_time'</span>, <span class="string">'create_time'</span>, &#123; <span class="attr">unique</span>: <span class="literal">false</span> &#125;); <span class="comment">// 标识快照的创建时间</span></span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 创建</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123;Object&#125;</span> </span>data </span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123;Function&#125;</span> </span>callback </span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  createData(data, callback) &#123;</span><br><span class="line">    <span class="comment">// 创建一个transaction(交易)，并指定其权限</span></span><br><span class="line">    <span class="keyword">const</span> transaction = <span class="keyword">this</span>.db.transaction(<span class="string">'history'</span>, <span class="string">'readwrite'</span>);</span><br><span class="line">    <span class="keyword">const</span> historyObjectStore = transaction.objectStore(<span class="string">'history'</span>)</span><br><span class="line">    historyObjectStore.add(&#123;</span><br><span class="line">      page_id: data.page_id,</span><br><span class="line">      data: data.data,</span><br><span class="line">      create_time: <span class="built_in">Date</span>.now(),</span><br><span class="line">    &#125;)</span><br><span class="line">    transaction.oncomplete = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">if</span> (<span class="keyword">typeof</span> callback === <span class="string">'function'</span>) callback()</span><br><span class="line">    &#125;</span><br><span class="line">    transaction.onerror = <span class="function"><span class="params">err</span> =&gt;</span> &#123;</span><br><span class="line">      message.error(<span class="string">'IndexedDB添加数据失败'</span>);</span><br><span class="line">      <span class="built_in">console</span>.warn(err);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 删除</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123;String&#125;</span> </span>pageId </span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123;Function&#125;</span> </span>callback </span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  deleteData(pageId, callback)&#123;</span><br><span class="line">    <span class="keyword">const</span> transaction = <span class="keyword">this</span>.db.transaction(<span class="string">'history'</span>, <span class="string">'readwrite'</span>);</span><br><span class="line">    <span class="keyword">const</span> historyObjectStore = transaction.objectStore(<span class="string">'history'</span>)</span><br><span class="line">    <span class="keyword">const</span> pageIdIndex = historyObjectStore.index(<span class="string">'page_id'</span>)</span><br><span class="line">    <span class="comment">// 利用page_id索引来打开一个图标</span></span><br><span class="line">    <span class="keyword">const</span> cursor = pageIdIndex.openKeyCursor(IDBKeyRange.only(pageId));</span><br><span class="line">    cursor.onsuccess = <span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">const</span> thisHistory = e.target.result;</span><br><span class="line">      <span class="keyword">if</span>(thisHistory) &#123;</span><br><span class="line">        thisHistory.delete()</span><br><span class="line">        thisHistory.continue();</span><br><span class="line">      &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">typeof</span> callback === <span class="string">'function'</span>) &#123;</span><br><span class="line">        transaction.abort()</span><br><span class="line">        callback()</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 查询</span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123;String&#125;</span> </span>pageId </span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123;Number&#125;</span> </span>time </span></span><br><span class="line"><span class="comment">   * <span class="doctag">@param <span class="type">&#123;Function&#125;</span> </span>callback </span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">  findData(pageId, time, callback) &#123;</span><br><span class="line">    <span class="keyword">const</span> transaction = <span class="keyword">this</span>.db.transaction(<span class="string">'history'</span>, <span class="string">'readwrite'</span>);</span><br><span class="line">    <span class="keyword">const</span> historyObjectStore = transaction.objectStore(<span class="string">'history'</span>)</span><br><span class="line">    <span class="keyword">const</span> createTimeIndex = historyObjectStore.index(<span class="string">'create_time'</span>)</span><br><span class="line">    <span class="comment">// 利用create_time索引来打开一个游标，查找所有小于这个时间点的数据</span></span><br><span class="line">    <span class="keyword">const</span> cursor = createTimeIndex.openCursor(IDBKeyRange.upperBound(time, <span class="literal">true</span>), <span class="string">'prev'</span>);</span><br><span class="line">    cursor.onsuccess = <span class="function"><span class="params">e</span> =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">const</span> thisHistory = e.target.result;</span><br><span class="line">      <span class="keyword">if</span>(thisHistory) &#123;</span><br><span class="line">        <span class="keyword">if</span> (thisHistory.value.page_id === pageId) &#123;</span><br><span class="line">          <span class="keyword">if</span> (<span class="keyword">typeof</span> callback === <span class="string">'function'</span>) &#123;</span><br><span class="line">            transaction.abort()</span><br><span class="line">            callback(thisHistory.value)</span><br><span class="line">          &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">          thisHistory.continue();</span><br><span class="line">        &#125;</span><br><span class="line">      &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">typeof</span> callback === <span class="string">'function'</span>) &#123;</span><br><span class="line">        transaction.abort()</span><br><span class="line">        callback()</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="结束"><a href="#结束" class="headerlink" title="结束"></a>结束</h3><p>参考资料</p>
<ol>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Web/API/IndexedDB_API/Using_IndexedDB" target="_blank" rel="noopener">MDN关于IndexedDB的介绍</a></li>
<li><a href="https://www.twle.cn/t/153#reply0" target="_blank" rel="noopener">IndexedDB简单教程</a></li>
</ol>

  </div>
  <div class="statement">
    <hr>  
    1. 如果本文帮到了您，不妨点一下右上角的”<i class="fas fa-share-alt"></i>“按钮。<br>
    2. 除非注明，本博文章均为原创，转载请以链接形式标明本文地址。<br>
    3. 如果你有任何疑问，欢迎发送邮件到我的邮箱<a href="mailto:andyliwr@outlook.com">andyliwr@outlook.com<br>
  </div>
</article>


    <div class="blog-post-comments">
        <div id="gitalk_thread">
            <noscript>加载评论需要在浏览器启用 JavaScript 脚本支持。</noscript>
        </div>
    </div>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#前言"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#IndexedDB的使用"><span class="toc-number">2.</span> <span class="toc-text">IndexedDB的使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#结束"><span class="toc-number">3.</span> <span class="toc-text">结束</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/&text=IndexedDB尝鲜"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/&title=IndexedDB尝鲜"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/&is_video=false&description=IndexedDB尝鲜"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=IndexedDB尝鲜&body=Check out this article: https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/&title=IndexedDB尝鲜"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/&title=IndexedDB尝鲜"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/&title=IndexedDB尝鲜"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/&title=IndexedDB尝鲜"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://andyliwr.github.io/2019/03/28/indexeddb_instroduce/&name=IndexedDB尝鲜&description="><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> 菜单</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> 目录</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> 分享</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> 返回顶部</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy; 2020 Dikang Li
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    </div>
</body>
</html>
<!-- styles -->
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
<link rel="stylesheet" href="/lib/justified-gallery/css/justifiedGallery.min.css">
<link rel="stylesheet" href="/lib/fancybox/jquery.fancybox.css">

<link rel="stylesheet" href="https://file.lantingshucheng.com/blog/v3/static/styles/gitalk.css">


<!-- jquery -->
<script src="/lib/jquery/jquery.min.js"></script>
<script src="/lib/justified-gallery/js/jquery.justifiedGallery.min.js"></script>
<script src="/lib/fancybox/jquery.fancybox.pack.js"></script>

<script type="text/javascript" src="https://file.lantingshucheng.com/blog/v3/static/scripts/md5.js"></script>
<script type="text/javascript" src="https://file.lantingshucheng.com/blog/v3/static/scripts/gitalk.min.js"></script>

<script src="/js/main.js"></script>
<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

    <script type="text/javascript">
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?7851ca7a86be957be994acb77639a5be";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();
    </script>

<!-- Disqus Comments -->


  <script type="text/javascript">
    window.onload = function(){
      if (window.location.href.indexOf('andyliwr.github.io') > -1) {
        var gitalk = new Gitalk({
          clientID: 'ad6f70df0c70d2b715f9',
          clientSecret: '40384bee244c1e17dd7c37733d21e9ef669e969c',
          repo: 'Andyliwr.github.io',
          owner: 'Andyliwr',
          admin: ['Andyliwr'],
          id: md5(location.pathname),
          distractionFreeMode: 'true'
        })
        gitalk.render('gitalk_thread')
      }
    }
  </script>


